﻿using StackExchange.Redis;
using System;

namespace RedisDemo
{
    public class RedisHelper
    {
        private static readonly ConnectionMultiplexer redisConnection;

        // 静态构造函数，初始化Redis连接
        static RedisHelper()
        {
            try
            {
                // 替换为你的Redis服务器地址和端口，如果有密码，使用 password=你的密码
                redisConnection = ConnectionMultiplexer.Connect("localhost:6379");
                Console.WriteLine("Redis 连接成功!");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Redis 连接失败: {ex.Message}");
            }
        }

        // 获取数据库实例
        public static IDatabase GetDatabase()
        {
            return redisConnection.GetDatabase();
        }

        // 获取发布订阅实例
        public static ISubscriber GetSubscriber()
        {
            return redisConnection.GetSubscriber();
        }
    }
}
